package ru.alperez.model;

import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import ru.alperez.util.Converter;
import ru.alperez.model.ExtendedLocation;

public class GpsPoint {
	private int order;
	private String name = null;
	private String description = null;
	private int localDate;
	private int localTime;
	private int nsat;
	private Creator creator;
	private ExtendedLocation location;
	
	public GpsPoint(){}
	
	public GpsPoint(ExtendedLocation location, Creator creator) {
		this.location = new ExtendedLocation(location);
		this.creator = creator;
		Date date = new Date();
		localDate = Converter.dDate2Int(date);
		localTime = Converter.tDate2IntSeconds(date);
	}
	
	
	public static GpsPoint fromJSON(JSONObject jPoint) {
		GpsPoint ret = null;
		try {
			ret = new GpsPoint();
			ret.order = jPoint.optInt("order");
			ret.name = jPoint.optString("name");
			ret.description = jPoint.optString("description");
			ret.localDate = jPoint.optInt("localDate");
			ret.localTime = jPoint.optInt("localTime");
			ret.nsat = jPoint.optInt("nsat");
			ret.creator = Creator.fromJSON(jPoint.optJSONObject("creator"));
			ret.location = ExtendedLocation.fromJSON(jPoint.optJSONObject("location"));
		} catch (NullPointerException e) {
			ret = null;
		}
		return ret;
	}
	
	public JSONObject toJSON() throws JSONException {
		JSONObject ret = new JSONObject();
		ret.put("order", order);
		ret.put("name", name);
		ret.put("description", description);
		ret.put("localDate", localDate);
		ret.put("localTime", localTime);
		ret.put("nsat", nsat);
		ret.put("creator", creator.toJSON());
		ret.put("location", location.toJSON());
		return ret;
	}
	
	// ------ Accessors ------
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getDescription() {
		return description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	public int getLocalDate() {
		return localDate;
	}
	
	public void setLocalDate(int localDate) {
		this.localDate = localDate;
	}
	
	public int getLocalTime() {
		return localTime;
	}
	
	public void setLocalTime(int localTime) {
		this.localTime = localTime;
	}
	
	public ExtendedLocation getLocation() {
		return location;
	}
	
	public void setLocation(ExtendedLocation location) {
		this.location = location;
	}

	public Creator getCreator() {
		return creator;
	}

	public void setCreator(Creator creator) {
		this.creator = creator;
	}

	public int getNsat() {
		return nsat;
	}

	public void setNsat(int nsat) {
		this.nsat = nsat;
	}

	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}
}
